<?php


namespace App\Http\Controllers\Saas;


use App\Contract\Platform;
use App\Http\Controllers\Controller;
use App\Http\Resources\PaginationCollection;
use App\Model\Staff;
use App\Utils\Result;
use App\Utils\Token;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

class StaffController extends Controller
{
    public function index(Request $request): \Illuminate\Http\JsonResponse
    {
        $page = $request->input('page');
        $pageSize = $request->input('pageSize');
        $query = Staff::query();
        $data = $query->paginate($pageSize, ['*'], 'page', $page);

        return Result::success(new PaginationCollection($data));
    }
    public function add(Request $request)
    {
        $data = $request->only([
            'name','password','mobile','roles'

        ]);
        $sp = new Staff();
        $sp->fill($data);
        $sp->password =Hash::make($data['password']);
        $sp->token=time();
        $sp->save();
        return Result::success();
    }

    public function edit(Request $request, $id)
    {
        $data = $request->only([
            'name','password','mobile','roles'
        ]);
        $sp = Staff::find($id);
        if(isset($data['password']) && $data['password']){
            $sp->password =Hash::make($data['password']);
        }
        unset($data['password']);
        $sp->fill($data);
        $sp->save();
        return Result::success();
    }

    public function show($id)
    {
        $sp = Staff::query()->where('id',$id)->first();
        return Result::success($sp);
    }



    public function store(Request $request)
    {
        $data = $request->only([
            'name',
            'mobile',
            'role_id',
            'department_id',
            'password',
            'is_disabled',
            'type'
        ]);
        $user = Token::getUser();
        if (Staff::where('mobile', $data['mobile'])->count()) {
            return Result::error(400, '手机号已被使用');
        }
        $staff = new Staff();
        $staff->fill($data);
        $staff->password =  Hash::make($data['password']);
        $staff->token = Str::random(16);
        $staff->save();

        return Result::success();
    }



    public function update(Request $request, $id)
    {
        $data = $request->only([
            'name',
            'mobile',
            'role_id',
            'department_id',
            'password',
            'is_disabled',
            'type'
        ]);
        $user = Token::getUser();
        $staff =  Staff::find($id);
        if (!$staff) {
            return Result::error(404, '账号不存在');
        }

        if (isset($data['mobile']) && Staff::where('id', '<>', $id)->where('mobile', $data['mobile'])->count()) {
            return Result::error(400, '手机号已被使用');
        }

        if (isset($data['password']) && $data['password']) {
            $staff->password =  Hash::make($data['password']);
            $staff->token = Str::random(16);
        }
        unset($data['password']);
        $staff->fill($data);
        if ($staff->is_disabled) {
            $staff->token = Str::random(16);
        }
        $staff->save();

        return Result::success();
    }

    public function destroy($id)
    {
        $res = Staff::find($id);
        if ($res) {
            $res->delete();
        }
        return Result::success();
    }
}
